/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.agi.myhome.servicos;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;

/**
 *
 * @author Renato
 */
public class ListadorEntidade<T> {
    
    private EntityManager em;
    public void setEntityManager(EntityManager em){
        this.em=em;
    }
    
    public ListadorEntidade<T> comEntityManager(EntityManager em){
        this.em=em;
        return this;
    }
    
    public List<T> getLista(Class<T> type){
        CriteriaBuilder cb;
        CriteriaQuery<T> criteria;
        TypedQuery<T> query;
        cb=em.getCriteriaBuilder(); 
        criteria=cb.createQuery(type);
        query=em.createQuery(criteria);        
        return query.getResultList();        
    }        
}
    
    
